home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / print-tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  12.1 KB  |  507 lines

  1. /* Prints out tree in human readable form - GNU C-compiler
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "tree.h"
  24. #include <stdio.h>
  25. #include "alloca.h"
  26.  
  27.  
  28. /* Names of tree components.
  29.    Used for printing out the tree and error messages.  */
  30. #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
  31.  
  32. char *tree_code_name[] = {
  33. #include "tree.def"
  34. };
  35. #undef DEFTREECODE
  36.  
  37. extern char *tree_code_type[];
  38. extern int tree_code_length[];
  39. extern char *mode_name[];
  40.  
  41. extern char spaces[];
  42.  
  43. #define MIN(x,y) ((x < y) ? x : y)
  44.  
  45. static FILE *outfile;
  46.  
  47. extern int tree_node_counter;
  48.  
  49. /* markvec[i] is 1 if node number i has been seen already.  */
  50.  
  51. static char *markvec;
  52.  
  53. static void dump ();
  54. void dump_tree ();
  55.  
  56. void
  57. debug_dump_tree (root)
  58.      tree root;
  59. {
  60.   dump_tree (stderr, root);
  61. }
  62.  
  63. void
  64. dump_tree (outf, root)
  65.      FILE *outf;
  66.      tree root;
  67. {
  68.   markvec = (char *) alloca (tree_node_counter + 1);
  69.   bzero (markvec, tree_node_counter + 1);
  70.   outfile = outf;
  71.   dump (root, 0);
  72.   fflush (outf);
  73. }
  74.  
  75. static
  76. void
  77. wruid (node)
  78.      tree node;
  79. {
  80.  
  81.   if (node == NULL)
  82.     fputs ("<>", outfile);
  83.   else {
  84.     fprintf (outfile, "%1d", TREE_UID (node));
  85.   }
  86. }
  87.  
  88. static 
  89. void
  90. part (title, node)
  91.      char title[];
  92.      tree node;
  93. {
  94.   fprintf (outfile, " %s = ", title);
  95.   wruid (node);
  96.   putc (';', outfile);
  97. }
  98.  
  99. /* Similar to `part' but prefix with @ if value is not constant
  100.    and print the constant value if it is constant.  */
  101. static
  102. void
  103. cpart (title, ct, punct)
  104.      char *title;
  105.      tree ct;
  106.      char punct;
  107. {
  108.   fprintf (outfile, " %s = ", title);
  109.   if (ct == NULL)
  110.     fputs ("<>", outfile);
  111.   else
  112.     {
  113.       if (!TREE_LITERAL (ct))
  114.     {
  115.       putc ('@', outfile);
  116.       wruid (ct);
  117.     }
  118.       else
  119.     fprintf (outfile, "%ld", TREE_INT_CST_LOW (ct));
  120.     }
  121.   putc(punct, outfile);
  122. }
  123.  
  124. static
  125. void
  126. walk (node, leaf, indent)
  127.      tree node;
  128.      tree leaf;
  129.      int indent;
  130. {
  131.   if (node != NULL
  132.       /* Don't walk any global nodes reached from local nodes!
  133.      The global nodes will be dumped at the end, all together.
  134.      Also don't mention a FUNCTION_DECL node that is marked local
  135.      since it was fully described when it was dumped locally.  */
  136.       && (TREE_CODE (node) != FUNCTION_DECL
  137.       || TREE_PERMANENT (node))
  138.       && (TREE_PERMANENT (leaf) == TREE_PERMANENT (node)))
  139.     dump (node, indent+1);
  140. }
  141.  
  142. static
  143. void
  144. cwalk (s, leaf, indent)
  145.      tree s;
  146.      tree leaf;
  147.      int indent;
  148. {
  149.   if (s != NULL) 
  150.     if (!TREE_LITERAL (s))
  151.       walk(s, leaf, indent);
  152. }
  153.  
  154. static
  155. void
  156. prtypeinfo (node)
  157.      register tree node;
  158. {
  159.   int first;
  160.   
  161.   part ("type", TREE_TYPE (node));
  162.   first = 1;
  163.   fputs (" [", outfile);
  164.   if (TREE_EXTERNAL (node))
  165.     {
  166.       if (!first) putc (' ', outfile);
  167.       fputs ("external", outfile);
  168.       first = 0;
  169.     }
  170.   if (TREE_PUBLIC (node))
  171.     {
  172.       if (!first) putc (' ', outfile);
  173.       fputs ("public", outfile);
  174.       first = 0;
  175.     }
  176.   if (TREE_STATIC (node))
  177.     {
  178.       if (!first) putc (' ', outfile);
  179.       fputs ("static", outfile);
  180.       first = 0;
  181.     }
  182.   if (TREE_VOLATILE (node))
  183.     {
  184.       if (!first) putc (' ', outfile);
  185.       fputs ("volatile", outfile);
  186.       first = 0;
  187.     }
  188.   if (TREE_PACKED (node))
  189.     {
  190.       if (!first) putc (' ', outfile);
  191.       fputs ("packed", outfile);
  192.       first = 0;
  193.     }
  194.   if (TREE_READONLY (node))
  195.     {
  196.       if (!first) putc (' ', outfile);
  197.       fputs ("readonly", outfile);
  198.       first = 0;
  199.     }
  200.   if (TREE_LITERAL (node))
  201.     {
  202.       if (!first) putc (' ', outfile);
  203.       fputs ("literal", outfile);
  204.       first = 0;
  205.     }
  206.   if (TREE_NONLOCAL (node))
  207.     {
  208.       if (!first) putc (' ', outfile);
  209.       fputs ("nonlocal", outfile);
  210.       first = 0;
  211.     }
  212.   if (TREE_ADDRESSABLE (node))
  213.     {
  214.       if (!first) putc (' ', outfile);
  215.       fputs ("addressable", outfile);
  216.       first = 0;
  217.     }
  218.   if (TREE_REGDECL (node))
  219.     {
  220.       if (!first) putc (' ', outfile);
  221.       fputs ("regdecl", outfile);
  222.       first = 0;
  223.     }
  224.   if (TREE_THIS_VOLATILE (node))
  225.     {
  226.       if (!first) putc (' ', outfile);
  227.       fputs ("this_vol", outfile);
  228.       first = 0;
  229.     }
  230.   if (TREE_UNSIGNED (node))
  231.     {
  232.       if (!first) putc (' ', outfile);
  233.       fputs ("unsigned", outfile);
  234.       first = 0;
  235.     }
  236.   if (TREE_ASM_WRITTEN (node))
  237.     {
  238.       if (!first) putc (' ', outfile);
  239.       fputs ("asm_written", outfile);
  240.       first = 0;
  241.     }
  242.   if (TREE_INLINE (node))
  243.     {
  244.       if (!first) putc (' ', outfile);
  245.       fputs ("inline", outfile);
  246.       first = 0;
  247.     }
  248.   fputs ("] ", outfile);
  249. }
  250.  
  251. static
  252. void
  253. prdeclmodeinfo(node)
  254.      tree node;
  255. {
  256.   register enum machine_mode mode = DECL_MODE (node);
  257.   fprintf (outfile, " %s;", mode_name[(int) mode]);
  258.  
  259.   cpart ("size", DECL_SIZE (node), '*');
  260.   fprintf (outfile, "%d;", DECL_SIZE_UNIT (node));
  261.  
  262.   fprintf (outfile, " alignment = %1d;", DECL_ALIGN (node));
  263. }
  264.  
  265. static
  266. void
  267. prtypemodeinfo(node)
  268.      tree node;
  269. {
  270.   register enum machine_mode mode = TYPE_MODE (node);
  271.   fprintf (outfile, " %s;", mode_name[(int) mode]);
  272.  
  273.   cpart ("size", TYPE_SIZE (node), '*');
  274.   fprintf (outfile, "%d;", TYPE_SIZE_UNIT (node));
  275.  
  276.   fprintf (outfile, " alignment = %1d;", TYPE_ALIGN (node));
  277. }
  278.  
  279. static
  280. void
  281. skip (indent)
  282.      int indent;
  283. {
  284.   putc ('\n',outfile);
  285.   fputs (spaces + (strlen (spaces) - (12 + MIN (40,(indent+1)*2))), outfile);
  286. }
  287.  
  288. /* Output a description of the tree node NODE
  289.    if its description has not been output already.  */
  290.  
  291. static 
  292. void
  293. dump (node, indent)
  294.      tree node;
  295.      int indent;
  296. {
  297.   register enum tree_code code = TREE_CODE (node);
  298.   register int i;
  299.   register int len;
  300.   int nochain = 0;
  301.  
  302.   if (markvec[TREE_UID (node)])
  303.     return;
  304.   markvec[TREE_UID (node)] = 1;
  305.  
  306.   fputs ("   ", outfile);
  307.   fprintf (outfile, "%5d", TREE_UID (node));
  308.   fputs (spaces + (strlen (spaces) - MIN (40, (indent+1)*2)), outfile);
  309.   fputs (tree_code_name[(int) code], outfile);
  310.  
  311.   switch (*tree_code_type[(int) code])
  312.     {
  313.     case 'd':
  314.       fputs (" name = ", outfile);
  315.       if (DECL_NAME (node) == NULL)
  316.     fputs("<>;", outfile);
  317.       else
  318.     fprintf (outfile, "%s;",
  319.          IDENTIFIER_POINTER (DECL_NAME (node)));
  320.       fprintf (outfile, " at %s line %d;",
  321.            DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
  322.       skip (indent);
  323.       prdeclmodeinfo (node);
  324.       prtypeinfo (node);
  325.       skip (indent);
  326.       fprintf (outfile, " offset = %1d;", DECL_OFFSET (node));
  327.       if (DECL_VOFFSET (node) != NULL)
  328.     {
  329.       fputs ("voffset = ", outfile);
  330.       wruid (DECL_VOFFSET (node));
  331.       fprintf (outfile, "*%1d;", DECL_VOFFSET_UNIT (node));
  332.     }
  333.       part ("context", DECL_CONTEXT (node));
  334.       if (DECL_ARGUMENTS (node) || DECL_RESULT (node)
  335.       || DECL_INITIAL (node))
  336.     {
  337.       skip(indent);
  338.       part ("arguments", DECL_ARGUMENTS (node));
  339.       part ("result", DECL_RESULT (node));
  340.       if ((int) (DECL_INITIAL (node)) == 1)
  341.         fprintf (outfile, " initial = const 1;");
  342.       else
  343.         part ("initial", DECL_INITIAL (node));
  344.     }
  345.       part ("chain", TREE_CHAIN (node));
  346.       /* A Decl's chain contents is not part of the decl.  */
  347.       nochain = 1;
  348.       fputc ('\n', outfile);
  349.       cwalk (DECL_SIZE (node), node, indent);
  350.       walk (TREE_TYPE (node), node, indent);
  351.       walk (DECL_VOFFSET (node), node, indent);
  352.       walk (DECL_CONTEXT (node), node, indent);
  353.       walk (DECL_ARGUMENTS (node), node, indent);
  354.       walk (DECL_RESULT (node), node, indent);
  355.       if ((int) (DECL_INITIAL (node)) != 1)
  356.     walk (DECL_INITIAL (node), node, indent);
  357.       break;
  358.  
  359.     case 't':
  360.       prtypemodeinfo (node);
  361.       prtypeinfo (node);
  362.       skip (indent);
  363.       part ("pointers_to_this", TYPE_POINTER_TO (node));
  364.       if (code == ARRAY_TYPE || code == SET_TYPE)
  365.     {
  366.       part ("domain", TYPE_DOMAIN (node));
  367.       cpart ("separation", TYPE_SEP (node), '*');
  368.       fprintf (outfile, "%d;", TYPE_SEP_UNIT (node));
  369.     }
  370.       else if (code == INTEGER_TYPE)
  371.     {
  372.       cpart ("min", TYPE_MIN_VALUE (node), ';');
  373.       cpart ("max", TYPE_MAX_VALUE (node), ';');
  374.       fprintf (outfile, "precision = %d;", TYPE_PRECISION (node));
  375.     }
  376.       else if (code == ENUMERAL_TYPE)
  377.     {
  378.       cpart ("min", TYPE_MIN_VALUE (node), ';');
  379.       cpart ("max", TYPE_MAX_VALUE (node), ';');
  380.       part ("values", TYPE_VALUES (node));
  381.       fprintf (outfile, "precision = %d;", TYPE_PRECISION (node));
  382.     }
  383.       else if (code == REAL_TYPE)
  384.     {
  385.       fprintf (outfile, "precision = %d;", TYPE_PRECISION (node));
  386.     }
  387.       else if (code == RECORD_TYPE
  388.            || code == UNION_TYPE)
  389.     {
  390.       part ("fields", TYPE_FIELDS (node));
  391.     }
  392.       else if (code == FUNCTION_TYPE)
  393.     {
  394.       part ("arg_types", TYPE_ARG_TYPES (node));
  395.     }
  396.       part ("chain", TREE_CHAIN (node));
  397.       /* A type's chain's contents are not printed because the chain of types
  398.      is not part of the meaning of any particular type.  */
  399.       nochain = 1;
  400.       fputc ('\n', outfile);
  401.       cwalk (TYPE_SIZE (node), node, indent);
  402.       walk (TREE_TYPE (node), node, indent);
  403.       walk (TYPE_VALUES (node), node, indent);
  404.       walk (TYPE_SEP (node), node, indent);
  405.       walk (TYPE_POINTER_TO (node), node, indent);
  406.       break;
  407.  
  408.     case 'e':
  409.     case 'r':
  410.       prtypeinfo (node);
  411.       fputs (" ops =", outfile);
  412.       len = tree_code_length[(int) code];
  413.       for (i = 0; i < len; i++)
  414.     {
  415.       fputs (" ", outfile);
  416.       wruid (TREE_OPERAND (node, i));
  417.       fputs (";", outfile);
  418.     }
  419.       part ("chain", TREE_CHAIN (node));
  420.       fputc ('\n', outfile);
  421.       walk (TREE_TYPE (node), node, indent);
  422.       for (i = 0; i < len; i++)
  423.     walk (TREE_OPERAND (node, i), node, indent);
  424.       break;
  425.  
  426.     case 's':
  427.       prtypeinfo (node);
  428.       fprintf (outfile, " at %s line %d;",
  429.            STMT_SOURCE_FILE (node), STMT_SOURCE_LINE (node));
  430.       fputs (" ops =", outfile);
  431.       len = tree_code_length[(int) code];
  432.       for (i = 0; i < len; i++)
  433.     {
  434.       fputs (" ", outfile);
  435.       wruid (TREE_OPERAND (node, i+2));
  436.       fputs (";", outfile);
  437.     }
  438.       part ("chain", TREE_CHAIN (node));
  439.       fputc ('\n', outfile);
  440.       walk (TREE_TYPE (node), node, indent);
  441.       for (i = 0; i < len; i++)
  442.     walk (TREE_OPERAND (node, i+2), node, indent);
  443.       break;
  444.  
  445.     case 'c':
  446.       switch (code)
  447.     {
  448.     case INTEGER_CST:
  449.       if (TREE_INT_CST_HIGH (node) == 0)
  450.         fprintf (outfile, " = %1u;", TREE_INT_CST_LOW (node));
  451.       else if (TREE_INT_CST_HIGH (node) == -1
  452.            && TREE_INT_CST_LOW (node) != 0)
  453.         fprintf (outfile, " = -%1u;", -TREE_INT_CST_LOW (node));
  454.       else
  455.         fprintf (outfile, " = 0x%x%08x;",
  456.              TREE_INT_CST_HIGH (node),
  457.              TREE_INT_CST_LOW (node));
  458.       break;
  459.  
  460.     case REAL_CST:
  461.       fprintf (outfile, " = %e;", TREE_REAL_CST (node));
  462.       break;
  463.  
  464.     case COMPLEX_CST:
  465.       part ("realpart", TREE_REALPART (node));
  466.       part ("imagpart", TREE_IMAGPART (node));
  467.       walk (TREE_REALPART (node), node, indent);
  468.       walk (TREE_IMAGPART (node), node, indent);
  469.       break;
  470.  
  471.     case STRING_CST:
  472.       fprintf (outfile, " = \"%s\";", TREE_STRING_POINTER (node));
  473.     }
  474.       prtypeinfo(node);
  475.       part ("chain", TREE_CHAIN (node));
  476.       fputc ('\n', outfile);
  477.       walk (TREE_TYPE (node), node, indent);
  478.       break;
  479.  
  480.     case 'x':
  481.       if (code == IDENTIFIER_NODE)
  482.     fprintf (outfile, " = %s;\n", IDENTIFIER_POINTER (node));
  483.       else if (code == TREE_LIST)
  484.     {
  485.       prtypeinfo (node);
  486.       part ("purpose", TREE_PURPOSE (node));
  487.       part ("value", TREE_VALUE (node));
  488.       part ("chain", TREE_CHAIN (node));
  489.       fputc ('\n', outfile);
  490.       walk (TREE_TYPE (node), node, indent);
  491.       walk (TREE_PURPOSE (node), node, indent);
  492.       walk (TREE_VALUE (node), node, indent);
  493.     }
  494.       else if (code == ERROR_MARK)
  495.     fputc ('\n', outfile);
  496.       else abort ();
  497.  
  498.       break;
  499.  
  500.     default:
  501.       abort ();
  502.     } /* switch */
  503.  
  504.   if (TREE_CHAIN (node) != NULL && ! nochain)
  505.     dump(TREE_CHAIN (node), indent);
  506. }
  507.